package com.sgolub.commanndsonline.web;
   
import java.security.Principal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.sgolub.commanndsonline.domain.entity.AdvertisingRegistry;
import com.sgolub.commanndsonline.helpers.GoogleMailTLSHelper;
import com.sgolub.commanndsonline.helpers.JsonDataWrapper;
import com.sgolub.commanndsonline.helpers.SecurityHelper;
import com.sgolub.commanndsonline.json.ui.UIMenu;
import com.sgolub.commanndsonline.service.entity.AdvertisingRegistryService;
 
@Controller
@RequestMapping("/json")
public class JSONController { 
	
	@Autowired
    private AdvertisingRegistryService advertisingRegistryService;
         
    @RequestMapping(value="/menu/{menuName}", method = RequestMethod.GET)
	public @ResponseBody UIMenu getUIMenuInJSON(@PathVariable String menuName,Principal principal) {
    	UIMenu menuItem = new UIMenu();
    	
    	if (SecurityHelper.getUserHighestRole(principal.getName()).equals("ROLE_ADMIN")) {
        	menuItem.setMenuName("Administrator Menu");
        	menuItem.setMenuItems(new String[]{"Reports", "User's Management"});
        	menuItem.setMenuContents(new String[]{"reports", "usersList"});
    		
    	} else if (SecurityHelper.getUserHighestRole(principal.getName()).equals("ROLE_USER")) {
        	menuItem.setMenuName("Registered User Menu");
        	menuItem.setMenuItems(new String[]{"Advetising Registry", "Basket", "Orders"});
        	menuItem.setMenuContents(new String[]{"link1", "link2", "link3"});
    	} 
    	
		return menuItem;
 
	}     

    @RequestMapping(value="/entity/{entityName}", method = RequestMethod.GET)
	public @ResponseBody JsonDataWrapper<AdvertisingRegistry> getAdvertisinRegistryListGET(WebRequest request) {    
    	System.out.println("GET - ARRAY SIZE IS:" + advertisingRegistryService.list().size());
    	 
    	System.out.println(request.toString());
    	 //get the current page posted from the grid.  
    	 int page = 1; //Integer.parseInt(request.getParameter("page"));  
    	 //get the list of objects to be displayed from the db calling the service  
    	 //advertisingRegistryService.list();
    	 //the total number of pages are dynamically calculated on the basis of the total number of rows in the table. get that.
    	 
    	 JsonDataWrapper<AdvertisingRegistry> jdw = 
    			 new JsonDataWrapper<AdvertisingRegistry>(page, 4, 
    					 advertisingRegistryService.list());  
    	 return jdw;  
    }    
    
    @RequestMapping(value="/entity/{entityName}", method = RequestMethod.POST)
	public @ResponseBody JsonDataWrapper<AdvertisingRegistry> getAdvertisinRegistryListPOST(WebRequest request) {    
    	System.out.println("POST - ARRAY SIZE IS:" + advertisingRegistryService.list().size());
    	System.out.println("POST- Request"+ request.toString());
    	System.out.println(request.getParameterMap().toString());
    	System.out.println("rp = " 			+ request.getParameter("rp"));
    	System.out.println("sortorder = " 	+ request.getParameter("sortorder"));
    	System.out.println("page = " 		+ request.getParameter("page"));
    	System.out.println("query = " 		+ request.getParameter("query"));
    	System.out.println("sortname = " 	+ request.getParameter("sortname"));
    	System.out.println("qtype = " 		+ request.getParameter("qtype"));
    	
    	 //get the current page posted from the grid.
    	int rp 		 	= Integer.parseInt(request.getParameter("rp"));
		String sortorder 	= request.getParameter("sortorder");
		int page		 	= Integer.parseInt(request.getParameter("page"));
		String query 		= request.getParameter("query");
		String sortname 	= request.getParameter("sortname");
		String qtype 		= request.getParameter("qtype");

		
		 
		//get the list of objects to be displayed from the db calling the service  
		List<AdvertisingRegistry> result = 
				advertisingRegistryService.advansedListWithPagination(rp, sortorder, page, query, sortname, qtype);
    	
		//the total number of pages are dynamically calculated on the basis of the total number of rows in the table. get that.
    	 
    	 JsonDataWrapper<AdvertisingRegistry> jdw = 
    			 new JsonDataWrapper<AdvertisingRegistry>(page, 
    					 advertisingRegistryService.list().size(),
    					 result);  
    	 
    	 GoogleMailTLSHelper ss = new GoogleMailTLSHelper();
    	 ss.Send("TESTING",request.getParameterMap().toString());
    	 return jdw;  
    }    
    
}



